package assists
{
	import assists.log.StringUtil;
	import assists.time.IntUtil;

	public class TimeToStr
	{
		public static function timeChangeToStr(time:Number):String
		{
			time = time / 1000;
			var minStr:String = int(time / 60) + "";
			var secStr:String = int(time % 60) + "";
			return addZero(minStr) + ":" + addZero(secStr);
		}
		
		private static function addZero(value:String):String
		{
			if(value.length == 1)
			{
				value = "0" + value;
			}
			return value;
		}
		
		
		
		public static const HOUR_TAG:String = '{!hour}';
		public static const MINUTE_TAG:String = '{!minute}';
		public static const SECOND_TAG:String = '{!second}';
		
		public static const DEFAULT_SECOND_FORMAT:String = HOUR_TAG + ":" + MINUTE_TAG + ":" + SECOND_TAG;
		
		public static function formatSecond(second:uint, format:String=null) : String
		{
			if (format == null) {
				format = DEFAULT_SECOND_FORMAT;
			}
			
			var hour:uint = Math.floor(second / 3600);
			var minute:uint = Math.floor((second - hour * 3600) / 60);
			 second = second % 60;
			
			var pairs:Object = {};
			pairs[HOUR_TAG] = IntUtil.format(hour, 2);
			pairs[MINUTE_TAG] = IntUtil.format(minute, 2);
			pairs[SECOND_TAG] = IntUtil.format(second, 2);
			return StringUtil.replace(format, pairs);			
		}
		
		public static function formatYearAndDay(second:uint, format:String=null):String
		{
			if (format == null) {
				format = DEFAULT_SECOND_FORMAT;
			}
			
			var time:Date = new Date(second * 1000);
			
			var year:uint = time.fullYear;
			var month:uint = time.month + 1;
			var day:uint = time.date;
			var hour:uint = time.hours;
			var mimutes:uint = time.minutes;
			var seconds:uint = time.seconds;
			
			var str:String = String(year) + "/" + String(month) + "/" + String(day);// + "-" + String(hour) + ":" + String(mimutes) + ":" + String(seconds);
			return str;
		}
		
		public static function formatYearAndDayInChinese(second:uint, format:String=null):String
		{
			if (format == null) {
				format = DEFAULT_SECOND_FORMAT;
			}
			
			var time:Date = new Date(second * 1000);
			
			var year:uint = time.fullYear;
			var month:uint = time.month + 1;
			var day:uint = time.date;
			var hour:uint = time.hours;
			var mimutes:uint = time.minutes;
			var seconds:uint = time.seconds;
			
			var str:String = String(year) + "年" + String(month) + "月" + String(day) + "日";// + "-" + String(hour) + ":" + String(mimutes) + ":" + String(seconds);
			return str;
		}
		
		public static function seperateDate(str:String):String
		{
			var date:String = "";
			
			date = str.slice(0,4) + "/" + str.slice(4,6) + "/" + str.slice(6,8);
			
			return date;
		}
	}
}